package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.service.SmsService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;


/**
 * @author wq
 * @TIME
 * @description 预约提交Controller
 */

@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class MobileOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private SetMealService setMealService;

    @Autowired
    private SmsService smsService;

    @GetMapping("/findById")
    public Result findById(Integer id) {
        log.info("[体检预约-订单数据展示,id:{}]", id);
        Order order = orderService.findById(id);
        Member member = memberService.findById(order.getMemberId());
        Setmeal setmeal = setMealService.findById(order.getSetmealId());
        Map<String, Object> map = new HashMap<>();
        map.put("member", member.getName());
        map.put("setmeal", setmeal.getName());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        map.put("orderDate", simpleDateFormat.format(order.getOrderDate()));
        map.put("orderType", order.getOrderType());
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, map);
    }


    @PostMapping("/submit")
    @Transactional
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam) {
        //提交的预约信息，需要存储到order表中和member中
        Order order = new Order();

        if (!smsService.checkValidateCode("ORDER", orderSubmitParam.getTelephone(), orderSubmitParam.getValidateCode())) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        Member creatMember = new Member();
        //判断用户是不是老用户，是的话直接拿到ID，不是的话新增该用户再拿到ID
        Member member = memberService.findByPhoneNumber(orderSubmitParam.getTelephone());
        if (member == null) {
            creatMember.setName(orderSubmitParam.getName());
            creatMember.setSex(orderSubmitParam.getSex());
            creatMember.setPhoneNumber(orderSubmitParam.getTelephone());
            creatMember.setIdCard(orderSubmitParam.getIdCard());
            memberService.addMember(creatMember);//需要先插入member,插入order时需要member的id
        } else {
            creatMember = member;
        }

        //该用户用手机登录过，但是没留下姓名，现在预约已经留下姓名，我们更新姓名进数据库
        if (creatMember.getName() == null) {
            creatMember.setName(orderSubmitParam.getName());
            memberService.updateById(creatMember);
        }
        //如果体检人信息的名字和登录用户的名字不一样的话，
        //更新member表(本项目只设计了一张member表，无法区分登录用户和体检人信息，我们默认是体检人信息表吧)
        if (!creatMember.getName().equals(orderSubmitParam.getName())) {
            creatMember.setName(orderSubmitParam.getName());
            memberService.updateById(creatMember);
        }
        log.info("order的id：{}", creatMember.getId());

        order.setMemberId(creatMember.getId());
        order.setOrderDate(orderSubmitParam.getOrderDate());
        order.setOrderType(Order.ORDERTYPE_TELEPHONE);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(orderSubmitParam.getSetMealId());
        orderService.addOrder(order);
        return new Result(true, MessageConst.ORDER_SUCCESS, order);
    }
}
